//
//  LWNewsController.m
//  练习项目
//
//  Created by 吴丹草 on 17/1/4.
//  Copyright © 2017年 吴丹草. All rights reserved.
//

#import "LWNewsController.h"
#import "CZAdditions.h"
#import "SZNewsNormalCell.h"
#import "SZNewsWidthImgCell.h"
#import "SZNewsMutiImgCell.h"
#import "SZNewsHeadCell.h"
#import "Masonry.h"
#import "SZNewsModel.h"

static NSString *normalIdentifier = @"normalIdentifier";
static NSString *widthImgIdentifier = @"widthImgIdentifier";
static NSString *mutiImgIdentifier = @"mutiImgIdentifier";
static NSString *headIdentifier = @"headIdentifier";


@interface LWNewsController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *newsList;


@end

@implementation LWNewsController

- (instancetype)initControllerWith : (NSInteger )index withModel : (LWChannelModel *)model
{
   if(self = [super init])
   {
       _index = index;
       _modle = model;
   }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cz_randomColor];
    _newsList = [NSMutableArray array];
    //设置tableView
    [self setupTableView];
    
}


- (void) setupTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //四种cell，注册四次
    [_tableView registerClass:[SZNewsNormalCell class] forCellReuseIdentifier:normalIdentifier];
    [_tableView registerClass:[SZNewsWidthImgCell class] forCellReuseIdentifier:widthImgIdentifier];
    [_tableView registerClass:[SZNewsMutiImgCell class] forCellReuseIdentifier:mutiImgIdentifier];
    [_tableView registerClass:[SZNewsHeadCell class] forCellReuseIdentifier:headIdentifier];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    //自动计算行高
    _tableView.estimatedRowHeight = 200;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    
    //设置tableView的内边距
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}


#pragma mark - UITableViewDataSource
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SZNewsModel *model = _newsList[indexPath.row];
    
    //代表它是首行
    if (model.hasHead == 1) {
        SZNewsHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:headIdentifier forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    //代表是大图
    else if (model.imgType == 1) {
        SZNewsWidthImgCell *cell = [tableView dequeueReusableCellWithIdentifier:widthImgIdentifier forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    //代表是三张图片
    else if (model.imgextra != nil) {
        SZNewsMutiImgCell *cell = [tableView dequeueReusableCellWithIdentifier:mutiImgIdentifier forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    //正常情况
    else {
        SZNewsNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:normalIdentifier forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _newsList.count;
}

#pragma mark - UITableViewDelegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SZNewsModel *model = _newsList[indexPath.row];
    NSLog(@"%@", model);
    
    //点击cell跳转到详细页
//    SZNewsDetailViewController *detail = [[SZNewsDetailViewController alloc] init];
//    detail.title = @"详情";
//    detail.model = model;
//    detail.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:detail animated:YES];
}


@end
